package com.huluobo.hyj.hyjmodel.ui.activity

import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import com.huluobo.hyj.hyjmodel.R
import com.huluobo.hyj.hyjmodel.contract.login.LoginContract
import com.huluobo.hyj.hyjmodel.contract.login.LoginPresenter
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/***
 * 实现登录接口的View
 * 布局变化
 */


class LoginActivity : BaseActivity() ,LoginContract.View{

    val presenter = LoginPresenter(this)

    override fun init() {
        super.init()
        //点击事件
       login.setOnClickListener {
           login()
       }
        //下一步的点击
        password.setOnEditorActionListener { v, actionId, event ->
            login()
            true
        }

        //注册页面的跳转
        newUser.setOnClickListener {
            startActivity<RegisterActivity>()
        }

    }

    //登录方法
    private fun  login(){
        //隐藏软键盘
        hideSoftKeyboard()
        //检查是否用一个写入的权限
        if(hasWriteExternalStoragePermission()){
            //获取用户名和密码
            val userNameString = userName.text.trim().toString()
            val passwordString = password.text.trim().toString()
            //调用presenter 传入参数
            presenter.login(userNameString,passwordString)
        }else applyWriteExternalStoragePermission()
    }

    //同意权限
    private fun hasWriteExternalStoragePermission():Boolean{
        //声明权限变量，拿到写入磁盘的权限
        val checkSelfPermission = ActivityCompat.checkSelfPermission(
            this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        //对比权限并返回，是否用户已经同意写入权限
        return checkSelfPermission == PackageManager.PERMISSION_GRANTED
    }
    private fun applyWriteExternalStoragePermission(){
        //获取权限组
        val permisssion = arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
        //去请求权限
        ActivityCompat.requestPermissions(this,permisssion,0)

    }
    //权限请求的判断
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //用户同意权限，开始登陆
        }else toast(R.string.permission_denied) //吐司权限
    }




    //登录用户名错误
    override fun onUserNameError() {
        userName.error = getString(R.string.user_name_error)
    }
    //登录密码错误
    override fun onUserpasswordError() {
        password.error = getString(R.string.password_error)
    }
    //进度条
    override fun onstartLogin() {
        showProgress(getString(R.string.logging))
    }
    //登录成功
    override fun onLoggedInSuccess() {
        //隐藏进度条
        dismissProgress()
        //进入主页面
        startActivity<MainActivity>()
        //退出当前Activity
        finish()
    }
    //登录失败
    override fun onLoggedInFailed() {
        //隐藏进度条
        dismissProgress()
        //弹出toast

             toast(getString(R.string.login_failed))

    }
    //得到布局
    override fun getLayoutResId(): Int = R.layout.activity_login
}